package example.movieRental.afterRF;

import java.util.Enumeration;
import java.util.Vector;

/**
 * Created by thinkpad on 2015/2/8.
 */
//Customer.java
//Customer类表示顾客，它还提供一个制造报表的方法
public class Customer {
    private String name;
    private Vector<Rental> rentals = new Vector<Rental>();

    public Customer(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void addRental(Rental r) {
        rentals.addElement(r);
    }

    public String statement() {
        Enumeration<Rental> rs = rentals.elements();
        String result = "Rental Record for" + this.getName() +"\n";
        while(rs.hasMoreElements()) {
            Rental each = (Rental)rs.nextElement();
            //累计积分
            //显示此次租赁的数据
            result += "\t" +each.getMovie().getTitle() + "\t" + String.valueOf(each.movie.getPrice().getCharge(each.getDaysRented())) + "\n";
        }

        result += "Amout owed is" + String.valueOf(getTotalCharge()) + "\n";
        result += "You earned" + String.valueOf(getTotalFrequentRenterPoints()) + "frequent renter points";
        return result;
    }

    public String htmlStatement() {
        Enumeration<Rental> rs = rentals.elements();
        String result = "<h1>Rental Record for<em>" + this.getName() +"</em></h1><p>\n";
        while(rs.hasMoreElements()) {
            Rental each = (Rental)rs.nextElement();
            //显示此次租赁的数据
            result += "\t" +each.getMovie().getTitle() + "\t" + String.valueOf(each.movie.getPrice().getCharge(each.getDaysRented())) + "<br>\n";
        }
        result += "<p>Amout owed is<em>" + String.valueOf(getTotalCharge()) + "</em></p>\n";
        result += "You earned<em>" + String.valueOf(getTotalFrequentRenterPoints()) + "</em>frequent renter points<p>";
        return result;
    }


    private double getTotalCharge() {
        double result = 0;
        Enumeration<Rental> rs = rentals.elements();
        while(rs.hasMoreElements()) {
            Rental each = (Rental)rs.nextElement();
            result += each.movie.getPrice().getCharge(each.getDaysRented());
        }
        return result;
    }

    private int getTotalFrequentRenterPoints() {
        int result = 0;
        Enumeration<Rental> rs = rentals.elements();
        while(rs.hasMoreElements()) {
            Rental each = (Rental)rs.nextElement();
            result += each.movie.getPrice().getFrequentRenterPoints(each.getDaysRented());
        }
        return result;
    }

}
